local skel = fk.CreateSkill {
  name = "lb__zhijianr",
  max_branches_use_time = {
    ["phase"] = {
      [Player.HistoryRound] = 1
    },
    ["damage"] = {
      [Player.HistoryRound] = 1
    },
  },
}

Fk:loadTranslationTable {
  ["lb__zhijianr"] = "致笺",
  --[":lb__zhijianr"] = "准备阶段，你可以展示一张手牌，横置任意名体力值之和不大于X的角色（X为此牌点数），然后你将之交给一名已横置角色。若X等于指定角色体力值和，本轮你发动<a href=':lb__jingsuo'><font color='red'>〖径梭〗</font></a>无次数限制。",

  --[":lb__zhijianr"] = "准备阶段，你可以展示一张手牌，横置任意名体力值之和不大于X的角色（X为此牌点数），然后你将之交给一名已横置角色。若X等于指定角色体力值和，你摸一张牌。",
  --9.2
  --[":lb__zhijianr"] = "准备阶段，你可以展示一张手牌，横置任意名体力值之和不大于X的角色（X为此牌点数），然后你将之交给一名已横置角色。若X等于指定角色体力值和，你回复1点体力。",
  --9.18
  --[":lb__zhijianr"] = "准备阶段，你可以摸一张牌并展示一张手牌，横置或重置任意名体力值之和不大于X的角色（X为此牌点数），然后你将之交给一名已横置角色。若X等于指定角色体力值和，你回复1点体力。",
  --9.23
  --[":lb__zhijianr"] = "每回合限一次，准备阶段或当你受到伤害后，你可以摸一张牌并展示一张手牌，横置或重置任意名体力值之和不大于X的角色（X为此牌点数），然后你将之交给一名已横置角色。若X等于指定角色体力值和，你回复1点体力。",
  [":lb__zhijianr"] = "每轮各限一次，准备阶段或当你受到伤害后，你可以摸一张牌并展示一张手牌，横置或重置任意名体力值之和不大于X的角色（X为此牌点数），然后你将之交给一名已横置角色。若X等于指定角色体力值和，你回复1点体力。",


  ["#lb__zhijianr"] = "致笺：你可以摸一张牌并展示一张手牌，横置或重置任意名体力值之和不大于此牌点数的角色",
  ["#lb__zhijianr_show"] = "致笺：请展示一张手牌，横置或重置任意名体力值之和不大于此牌点数的角色",
  ["#lb__zhijianr-chain"] = "致笺：横置或重置任意名体力值之和不大于 %arg 的角色",
  ["#lb__zhijianr-give"] = "致笺：将 %arg 交给一名已横置角色",

  ["@lb__jingsuo-round"] = "径梭",
  ["BypassTimes"] = "无次数限制",

  ["$lb__zhijianr1"] = "灵魂絮语，化成万片飞去。",
  ["$lb__zhijianr2"] = "有我们在，就不怕迷路啦。",
}
--[[
skel:addEffect(fk.EventPhaseStart, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and target == player and data.phase == Player.Start and not player:isKongcheng()
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local cards = room:askToCards(player, {
      min_num = 1,
      max_num = 1,
      skill_name = skel.name,
      cancelable = true,
      prompt = "#lb__zhijianr",
      include_equip = false,
    })
    event:setCostData(self, { cards = cards })
    return #cards > 0
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local card = Fk:getCardById(event:getCostData(self).cards[1])
    local n = card.number
    player:showCards(card)
    local tos = {}
    local draw = false
    local default = table.find(room:getAlivePlayers(), function(t)
      return t.hp <= n and not t.chained
    end)
    if default then
      local use_data = {
        skillName = skel.name,
        extra_data = {
          number = n
        },
      }
      local success, ret = room:askToUseActiveSkill(player, {
        skill_name = "#lb__zhijianr_choose",
        prompt = "#lb__zhijianr-chain:::" .. card.number,
        cancelable = false,
        extra_data = use_data,
      })
      if success and ret and ret.targets then
        tos = table.simpleClone(ret.targets)
      else
        table.insert(tos, default)
      end
      room:sortByAction(tos)
      room:doIndicate(player, tos)
      local hp = 0
      for _, t in ipairs(tos) do
        hp = hp + t.hp
        if not t.dead and not t.chained then
          t:setChainState(true)
        end
      end
      --[[
      if hp == n then
        room:setPlayerMark(player,"@lb__jingsuo-round","BypassTimes")
      end
      --]
      draw = hp == n -- 8.30
    end
    tos = table.filter(room:getOtherPlayers(player), function(t)
      return t.chained
    end)
    if #tos == 0 then return end
    local t = room:askToChoosePlayers(player, {
      targets = tos,
      min_num = 1,
      max_num = 1,
      skill_name = skel.name,
      cancelable = false,
      prompt = "#lb__zhijianr-give:::" .. card:toLogString(),
    })[1]
    room:obtainCard(t, card, true, fk.ReasonGive, player, skel.name)
    if draw and player:isWounded() then
      --player:drawCards(1, skel.name) -- 8.30
      room:recover { -- 9.2
        who = player,
        num = 1,
        skillName = skel.name,
      }
    end
  end,
})
--]]

local zhijianr_spec = {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(skel.name) and target == player then
      if event == fk.EventPhaseStart then
        return data.phase == Player.Start and skel:withinBranchTimesLimit(player, "phase", Player.HistoryRound)
      else
        return skel:withinBranchTimesLimit(player, "damage", Player.HistoryRound)
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askToSkillInvoke(player, { skill_name = skel.name, prompt = "#lb__zhijianr" })
  end,
  ---@param player ServerPlayer
  on_use = function(self, event, target, player, data)
    player:addSkillBranchUseHistory(skel.name, event == fk.EventPhaseStart and "phase" or "damage")
    local room = player.room
    player:drawCards(1, skel.name)
    if player:isKongcheng() then return end
    local cards = room:askToCards(player, {
      min_num = 1,
      max_num = 1,
      skill_name = skel.name,
      cancelable = false,
      prompt = "#lb__zhijianr_show",
      include_equip = false,
    })
    if #cards == 0 then return end
    local card = Fk:getCardById(cards[1])
    local n = card.number
    player:showCards(card)
    local tos = {}
    local draw = false -- 是否摸牌，改回血了
    local default = table.find(room:getAlivePlayers(), function(t)
      return t.hp <= n and not t.chained
    end)
    local default_choice = "chained"
    if default == nil then
      default_choice = "un-chained"
      default = table.find(room:getAlivePlayers(), function(t)
        return t.hp <= n and t.chained
      end)
    end
    if default then
      local use_data = {
        skillName = skel.name,
        extra_data = {
          number = n
        },
      }
      local success, ret = room:askToUseActiveSkill(player, {
        skill_name = "#lb__zhijianr_choose",
        prompt = "#lb__zhijianr-chain:::" .. card.number, --偷个懒，不分别提示了
        cancelable = false,
        extra_data = use_data,
      })
      if success and ret and ret.targets and ret.interaction then
        default_choice = ret.interaction
        tos = table.simpleClone(ret.targets)
      else
        table.insert(tos, default)
      end
      room:sortByAction(tos)
      room:doIndicate(player, tos)
      local hp = 0
      for _, t in ipairs(tos) do
        hp = hp + t.hp
        if not t.dead then
          if t.chained and default_choice == "un-chained" then
            t:setChainState(false)
          elseif not t.chained and default_choice == "chained" then
            t:setChainState(true)
          end
        end
      end
      draw = hp == n -- 8.30
    end
    tos = table.filter(room:getOtherPlayers(player), function(t)
      return t.chained
    end)
    if #tos > 0 and table.contains(player:getCardIds("h"), card.id) then
      local to = tos[1]
      if #tos > 1 then
        to = room:askToChoosePlayers(player, {
          targets = tos,
          min_num = 1,
          max_num = 1,
          skill_name = skel.name,
          cancelable = false,
          prompt = "#lb__zhijianr-give:::" .. card:toLogString(),
        })[1]
      end
      room:obtainCard(to, card, true, fk.ReasonGive, player, skel.name)
    end
    if draw and player:isWounded() then
      --player:drawCards(1, skel.name) -- 8.30
      room:recover { -- 9.2
        who = player,
        num = 1,
        skillName = skel.name,
        recoverBy = player,
      }
    end
  end,
}

skel:addEffect(fk.EventPhaseStart, zhijianr_spec, { check_skill_limit = true})
skel:addEffect(fk.Damaged, zhijianr_spec, { check_skill_limit = true})

return skel
